package com.jiang.controller;


import com.jiang.pojo.Emp;
import com.jiang.pojo.Result;
import com.jiang.service.EmpService;
import com.jiang.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {


    @Autowired
    private EmpService empService;

    // 登录请求处理
    @RequestMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("登录请求：{}，{}",emp.getUsername(),emp.getPassword());
        Emp e = empService.login(emp);


        // 登录成功 生成令牌 下发令牌
        if(e != null){
            // 生成当前员工信息
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());

            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }

        // 登录失败 返回错信息
        return Result.error("密码或用户名错误");



    }

}
